package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }



    //新增会员
    public void add(Member member) {
        if(member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    //根据月份统计会员数量
    public List<Integer> findMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        for(String m : month){
            m = m + ".31";//格式：2019.04.31
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }

    @Override
    public Map getMemberAge() {
        Map<String, List> map = new HashMap<>();
        String older="老年人";
        String younger="青年人";
        String children="孩子";
        ArrayList<String> nameList = new ArrayList<>();
        nameList.add(older);
        nameList.add(younger);
        nameList.add(children);
        map.put("ages",nameList);
        Calendar calendar0 = Calendar.getInstance();
        calendar0.add(Calendar.YEAR,-100);
        String time00 = new SimpleDateFormat("yyyy.MM.dd").format(calendar0.getTime());

        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.YEAR,-50);
        String time50 = new SimpleDateFormat("yyyy.MM.dd").format(calendar1.getTime());

        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.YEAR,-18);
        String time18 = new SimpleDateFormat("yyyy.MM.dd").format(calendar2.getTime());

        Calendar calendar3 = Calendar.getInstance();
        String timeNow = new SimpleDateFormat("yyyy.MM.dd").format(calendar3.getTime());

        ArrayList<Integer> countList = new ArrayList<>();
        Integer olderCount=memberDao.getCountByDate(time00,time50);
        Integer youngerCount=memberDao.getCountByDate(time50,time18);
        Integer childrenCount=memberDao.getCountByDate(time18,timeNow);
        countList.add(olderCount);
        countList.add(youngerCount);
        countList.add(childrenCount);
        map.put("count",countList);
        return map;
    }

    /*
    * gjx
    * */
    //根据id修改会员信息
    public void edit(Member member) {
        memberDao.edit(member);
    }

    //根据id修改电话
    public void updateTelephoneById(Integer id, String telephone) {
        Member member = new Member();
        member.setId(id);
        member.setPhoneNumber(telephone);
        this.memberDao.updateTelephoneById(member);
    }

    //提交新密码
    public void updatePasswordByTelephone(String telephone, String newPassword) {
        Member member = new Member();
        member.setPassword(MD5Utils.md5(newPassword));
        member.setPhoneNumber(telephone);
        memberDao.updatePasswordByTelephone(member);
    }



   //会员分页查询
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }


    //删除会员
    public void delete(Integer id) {
        memberDao.deleteById(id);
    }


    public Member findById(Integer id) {
        return memberDao.findById(id);
    }
    //
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        PageHelper.startPage(currentPage,pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面
        Page<Member> page = memberDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<String> getNames() {
        return memberDao.getNames();
    }
}
